//
//  SYDRetrievePayPasswordController.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/14.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "SYDRetrievePayPasswordController.h"
#import "SYDNetwork.h"
#import "YMRegexUtils.h"

@interface SYDRetrievePayPasswordController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UIButton *timeBtn;
@property (weak, nonatomic) IBOutlet UIButton *backBtn;//找回密码
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnConstraint;

@end

@implementation SYDRetrievePayPasswordController
#pragma mark - 导航栏
- (void)navigationBarHandle{
    
    WEAKSELF
    [weakSelf backButtonAction:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationBar.title = @"找回支付密码";
    [self navigationBarHandle];
    [self initRetrievePayPasswordUI];
}
-(void)initRetrievePayPasswordUI{
    if (iPhone8_X) {
        self.topConstraint.constant = 88;
        self.btnConstraint.constant = 58;
    }else{
        self.topConstraint.constant = 64;
        self.btnConstraint.constant = 48;
    }
//    self.phoneTextField.placeholderColor = GRAY_COLOR;
//    self.codeTextField.placeholderColor = GRAY_COLOR;
    self.timeBtn.eventTimeInterval = 1.5;
    [self.timeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.timeBtn setTitleColor:GREEN_COLOR forState:UIControlStateNormal];
    
    WEAKSELF
#pragma mark -- 监听事件(按钮点击)
    [[self.backBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSLog(@"点击了找回密码");
        [weakSelf verificationPaySMSCode];
    }];
    
    [[self.timeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf getVerificationCodeFromServer];
        }];
    
    #pragma mark---监听textfield 输入文字
    if (self.phoneTextField.text.length > 0) {
        [self.phoneTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
            NSLog(@"电话号码1111---%@",x);
        }];
    }
    if (self.codeTextField.text.length > 0) {
        [self.codeTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
            NSLog(@"验证码---%@",x);
        }];
    }
    //通知
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardDidShowNotification object:nil]subscribeNext:^(NSNotification * _Nullable x) {
        NSLog(@"键盘弹起");
    }];
}
#pragma mark---获取短信验证码
-(void)getVerificationCode{
    if (self.phoneTextField.text.length == 0) {
        [SVProgressHUD displayInfoWithStatus:@"请输入手机号"];
    }else if (![NSString justMobile:self.phoneTextField.text]) {
        [SVProgressHUD displayInfoWithStatus:@"当前输入的电话号码不合法"];
    }else{
        [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneTextField.text zone:@"86" result:^(NSError *error) {
            
            if (!error)
            {
                // 请求成功
                NSLog(@"获取验证码成功");
                [SVProgressHUD displayInfoWithStatus:@"获取验证码成功"];
            }
            else
            {
                // error
                NSLog(@"获取验证码失败,%ld",error.code);
            }
        }];
    }
}

- (void)getVerificationCodeFromServer{
    
    NSString * phone = self.phoneTextField.text;
    
    if (phone.length == 0) {
        [SVProgressHUD displayInfoWithStatus:@"手机号不能为空"];
    }else if (![YMRegexUtils isMobile:phone]) {
        [SVProgressHUD displayInfoWithStatus:@"请输入正确手机号"];
    }else{
        [self openCountDown];
        [[SYDNetwork shareTool] getPaySmsCodeWithMobile:self.phoneTextField.text success:^(NSURLSessionDataTask *task, id responseObject) {
            @try{
                if (![responseObject[@"success"] boolValue]) {
                    [SVProgressHUD displayInfoWithStatus:responseObject[@"msg"]];
                }
            }@catch(NSException * e){
                
            }
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
    }
}

- (void)verificationPaySMSCode{
    NSString * phone = self.phoneTextField.text;
    NSString * code = self.codeTextField.text;
    if (phone.length == 0) {
        [SVProgressHUD displayInfoWithStatus:@"手机号不能为空"];
        return;
    }
    
    if (![YMRegexUtils isMobile:phone]) {
        [SVProgressHUD displayInfoWithStatus:@"请输入正确的手机号"];
        return;
    }
    
    if (code.length == 0) {
        [SVProgressHUD displayInfoWithStatus:@"验证码不能为空"];
        return;
    }
    
    if (code.length < 6) {
        [SVProgressHUD displayInfoWithStatus:@"请输入正确的验证码"];
        return;
    }
    
    WEAKSELF
    [[SYDNetwork shareTool] verificationPaySmsWithMobile:self.phoneTextField.text sms:self.codeTextField.text success:^(NSURLSessionDataTask *task, id responseObject) {
        @try{
            if (![responseObject[@"success"] boolValue]) {
                [SVProgressHUD displayInfoWithStatus:responseObject[@"msg"]];
            }else{
                SYDSetThepaymentController *setVc = [[SYDSetThepaymentController alloc]init];
                [weakSelf.navigationController pushViewController:setVc animated:YES];
            }
        }@catch(NSException * e){
            
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}



#pragma mark---获取验证码倒计时
-(void)openCountDown{
    
    __block NSInteger time = 59;//倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _time = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_time, dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC, 0);//每秒执行
    
    dispatch_source_set_event_handler(_time, ^{
        
        if (time <= 0) {
            dispatch_source_cancel(_time);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.timeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
                [self.timeBtn setTitleColor:GREEN_COLOR forState:UIControlStateNormal];
                self.timeBtn.userInteractionEnabled = YES;
            });
        }else{
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.timeBtn setTitle:[NSString stringWithFormat:@"%.2ds后重新获取",seconds] forState:UIControlStateNormal];
                [self.timeBtn setTitleColor:GREEN_COLOR forState:UIControlStateNormal];
                self.timeBtn.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_time);
}

@end





